package io.summer.basis.async;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.lang.NonNull;

import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * @author Moonlight
 */
public class AsyncExceptionHandler implements AsyncUncaughtExceptionHandler {
  private static final Logger log = LoggerFactory.getLogger(AsyncExceptionHandler.class);

  @Override
  public void handleUncaughtException(@NonNull Throwable ex, @NonNull Method method, Object... params) {
    if (log.isErrorEnabled()) {
      log.error("Async method has uncaught exception, method: {}, params:{}",
        method.getName(), Arrays.toString(params));
    }
    if (ex instanceof AsyncException) {
      AsyncException asyncException = (AsyncException) ex;
      log.error("asyncException: {}", asyncException.getMessage());
    } else {
      log.error("Exception :", ex);
    }
  }
}
